<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible">
    <el-form
      ref="form"
      :model="form"
      label-suffix="："
      label-width="120px">
      <el-form-item
        label="公告标题"
        prop="title"
        :rules="[{required: true, message:'请输入标题'}]">
        <el-input
          size="small"
          v-model="form.title"
          placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item
        label="发布地区"
        :rules="[{required: true, message: '请选择发布地区'}]"
      >
        <el-select
          size="small"
          v-model="form.area"
          :disabled="isEdit"
        >
          <el-option label="铜陵" value="铜陵"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="公告内容"
        prop="content"
        :rules="[{required: true, message:'请输入内容'}]">
        <el-input
          type="textarea"
          :rows="5"
          v-model="form.content"
          placeholder="请输入内容"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        size="small"
        @click="dialogVisible = false"
      >
        取消
      </el-button>
      <el-button
        size="small"
        :loading="submit_loading"
        @click="confirmClick"
        type="primary"
      >
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>/**
 * @Author: lifuzhao
 * @Date: 2019-07-15
 * @Project: driver
 */
export default {
  name: "edit"
  , data() {
    return {
      dialogVisible: false
      , form: {
        title: '',
        area: '铜陵',
        content: '',
      }
      , submit_loading: false
      , announcement: {}
    }
  }
  , computed: {
    isEdit() {
      let announcement = this.announcement
      return announcement.id && announcement.id > 0
    }
    , title() {
      return this.isEdit ? '正在编辑 ' + this.announcement.title : '新增公告'
    }
    , url() {
      return this.isEdit ? '/api/v1/notice/update' : '/api/v1/notice/save'
    }
  }
  , watch: {
    dialogVisible(bool) {
      this.$fn.setTitle(bool ? this.title : '公告管理')
    }
  }
  , mounted() {
    this.init()
  }
  , methods: {
    init() {
      this.$bus.$on('announcement-edit', announcement => {
        this.dialogVisible = true
        this.announcement = announcement
        Object.keys(this.form).forEach(key => {
          this.form[key] = announcement[key] || this.form[key]
        })
      })
    }
    , confirmClick() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        let form = this.$fn.deepClone(this.form)
          , announcement = this.announcement
        if (this.isEdit) {
          form.id = announcement.id
          delete form.area
        } else {
          form.from = 'pc'
        }
        this.submit_loading = true
        this.$axios.post(this.url, form)
          .then(res => {
            let {success, msg} = res
            if (success) {
              this.$message({
                message: `操作成功`
                , type: 'success'
              })
              this.dialogVisible = false
              this.$bus.$emit('announcement-list-update')
            } else {
              this.$message({
                message: `操作失败` + msg
                , type: 'error'
              })
            }
          })
          .catch(() => {
            this.$message({
              message: `操作失败` + msg
              , type: 'error'
            })
          })
          .finally(() => {
            this.submit_loading = false
          })
      })
    }
  }
}
</script>

<style scoped>

</style>